﻿using eProcurement.BLL;
using eProcurement.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class Settings2 : System.Web.UI.Page
    {
        Manager manager = new Manager();

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                Purchaser user = (Purchaser)manager.GetAppUser( Context.User.Identity.Name );
                txtUsername.InnerText = user.Username;
                txtpAddress1.Value = user.pAddress1;
                txtpAddress2.Value = user.pAddress2;
                txtpCity.Value = user.pCity;
                txtYear.Value = user.ExpYear;
                txtMonth.Value = user.ExpMonth;
                txtCC.Value = "****-****-****-" + user.CC.Substring( ( user.CC.Length - 4 ), 4 );
                txtCode.Value = user.Code;
            }
        }

        protected void btnSave_Click( object sender, EventArgs e )
        {
            bool isformgood = true;
            AppUser user = manager.GetAppUser( Context.User.Identity.Name );

            if ( txtCPassword.Value != string.Empty || txtOldPassword.Value != string.Empty || txtPassword.Value != string.Empty )
            {
                lblPasswordMatchError.Visible = false;
                errpwdlength.Visible = false;
                lblOldPwdError.Visible = false;

                if ( user.Password != txtOldPassword.Value )
                {
                    isformgood = false;
                    lblOldPwdError.Visible = true;
                }

                if ( txtPassword.Value.Trim() == string.Empty || txtCPassword.Value.Trim() == string.Empty || txtPassword.Value != txtCPassword.Value )
                {
                    isformgood = false;
                    lblPasswordMatchError.Visible = true;
                }

                if ( txtPassword.Value.Count() < 6 )
                {
                    isformgood = false;
                    errpwdlength.Visible = true;
                }
            }

            if ( isformgood )
            {
                Purchaser puser = new Purchaser();

                if ( !txtCC.Value.Contains( "*" ) )
                {
                    puser.CC = txtCC.Value;
                }
                puser.ExpMonth = txtMonth.Value;
                puser.ExpYear = txtYear.Value;
                puser.pFirstname = txtpFirstName.Value;
                puser.pLastName = txtpLastName.Value;
                puser.Code = txtCode.Value;
                puser.pAddress1 = txtpAddress1.Value;
                puser.pAddress2 = txtpAddress2.Value;
                puser.pCity = txtpCity.Value;

                if ( manager.UpdatePayment( puser ) )
                {
                    tipSaved.Visible = true;
                }
            }
        }
    }
}